package com.hainatong.jcmvvmbase.mainhome.fragment;

import android.os.Bundle;
import android.view.View;
import cn.bertsir.zbar.Qr.ScanResult;
import cn.bertsir.zbar.QrManager;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.hainatong.jcmvvmbase.App;
import com.hainatong.jcmvvmbase.Constants;
import com.hainatong.jcmvvmbase.MvvmLazyFragment;
import com.hainatong.jcmvvmbase.R;
import com.hainatong.jcmvvmbase.databinding.FragmentShelfBinding;
import com.hainatong.jcmvvmbase.mainhome.ComFragmentActivity;
import com.hainatong.mvvmbase.base.NoViewModel;

public class ShelfFragment extends MvvmLazyFragment<FragmentShelfBinding , NoViewModel>
{
    private static String SELF_TAG = "ShelfFragment";

    private String selfCode ;

    public static ShelfFragment newInstance(String scanCode)
    {
        Bundle bundle = new Bundle();
        bundle.putString( SELF_TAG , scanCode );
        ShelfFragment shelf = new ShelfFragment();
        shelf.setArguments( bundle );
        return  shelf;
    }

    @Override
    public int getLayoutId()
    {
        return R.layout.fragment_shelf;
    }

    @Override
    public int getBindingVariable()
    {
        return 0;
    }

    @Override
    protected void onRetryBtnClick()
    {
    }

    @Override
    protected void initParameters()
    {
        Bundle args = getArguments();
        if(  args != null )
        {
            selfCode = args.getString(SELF_TAG , "");
        }
    }

    @Override
    protected void initView(View view)
    {


        viewDataBinding.btnStartRuku.setOnClickListener( v -> {

            if( !StringUtils.isEmpty( selfCode ) )
            {
                ToastUtils.showShort( selfCode );
            }
            startScanQrcode();

        } );
    }

    @Override
    protected void lazyLoad()
    {
           //根据   selfCode 查询 货架 信息
    }

    public void startScanQrcode()
    {
        QrManager.getInstance().init( App.getScanQrConfigByType(2) ).startScan(getActivity(), new QrManager.OnScanResultCallback() {
            @Override
            public void onScanSuccess(ScanResult result)
            {
                LogUtils.e(TAG, "onScanSuccess: " + result.getContent());
                if( result.getType() == ScanResult.CODE_BAR)
                {
                    //ToastUtils.showShort( result.getContent() );

                    // 跳转 入库 详情
                    Bundle bundle = new Bundle();
                    bundle.putString( Constants.FRAGMNET_TYPE, Constants.VIEW_TYPE_RUKU_TASK);
                    bundle.putString( Constants.SCAN_CONTENT_CODE, result.getContent());
                    ComFragmentActivity.start( getActivity(),bundle );
                }
                else
                {
                    ToastUtils.showShort( result.getContent() );
                }
            }
        });
    }

}
